import {Component, Input, OnInit, ViewChild} from '@angular/core';
import {CommonPage} from '../common-page';
import {DhlTableComponent} from '../../../shared/components/dhlcomponent/table/dhl-table.component';
import {UpdateUserFormComponent} from './updateForm/update.user.form.component';
import {ProjectUserFormComponent} from './form/project.user.form.component';
import {CacheKey, SelectType} from '../../../shared/components/dhlcomponent/enums';

@Component({
  templateUrl: './project.user.component.html'
})
export class ProjectUserComponent extends CommonPage implements OnInit {

  formComponent = ProjectUserFormComponent;
  formTitle = `更新-{name}`;

  colWidths = [[5, 5, 5]];
  name;
  userNo;
  email;
  showEdit = true;
  showAdd = true;
  showDelete = true;
  @Input() showDataPer = false;
  @Input() projectCode;
  @Input() selectType = SelectType.DEFAULT;
  @Input() projectId;
  @ViewChild('table', {static: true, read: DhlTableComponent}) tableRef: DhlTableComponent;


  editClick(row) {
    this.requestService.get(`projectuser/get/${row.id}`, {
      joinDomain: ['roles', 'dataPerTrees'],
      projectId: this.projectId
    }).subscribe(data => {
      if (data) {
        this.windowService.openWindow({
          submitUrl: `projectuser/save`,
          component: UpdateUserFormComponent,
          title: `更新-${row.name}`,
          okText: '更新',
          data: {
            ...data,
            userIds: data.id,
            projectId: this.projectId,
            projectCode: this.projectCode,
            selectType: this.projectCode == 'rpmp' ? SelectType.DEFAULT : SelectType.MULTIPLE,
            showDataPer: this.showDataPer
          },
          afterOk: () => {
            this.tableRef.freshTable();
          }
        });
      }
    });
  }

  ngOnInit(): void {
    const {isSuperManager} = this.cacheService.getItem(CacheKey.LOGIN_USER);
    const projects = this.cacheService.getItem(CacheKey.MANAGE_PROJECTS);
    if (!isSuperManager) {
      if (projects) {
        for (const p of projects) {
          if (p.code === this.projectCode) {
            if (!p.manager) {
              const {addUserPer, updateUserPer, deleteUserPer} = p;
              this.showAdd = addUserPer;
              this.showEdit = updateUserPer;
              this.showDelete = deleteUserPer;
            }
          }
        }
      }
    }
  }


}




